<template>
  <div class="one-tab-container" v-scroll.smooth="index">
    <div
      class="tab-item"
      v-for="(item, i) in menuList"
      :key="i"
      :class="{ active: index == i }"
      @touchend="scrollTo(i)"
      @touchmove="move = true"
      @touchstart="move = false"
    >
      <div class="img-container">
        <img :src="item.imgURL" :alt="item.title" />
      </div>
      <div class="tab-title">
        <span>{{ item.title }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import scroll from '@/directives/auto_scroll';

export default {
  data() {
    return {
      move: false,
      index: 0,
      menuList: [
        {
          title: '时令水果',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/时令水果.jpg',
        },
        {
          title: '酒水冲调',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/酒水冲调.jpg',
        },
        {
          title: '安心乳品',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/安心乳品.jpg',
        },
        {
          title: '休闲零食',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/休闲零食.jpg',
        },
        {
          title: '肉蛋食材',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/肉蛋食材.jpg',
        },
        {
          title: '熟食餐饮',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/熟食餐饮.jpg',
        },
        {
          title: '海鲜水产',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/海鲜水产.jpg',
        },
        {
          title: '家居收纳',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/家居收纳.jpg',
        },
        {
          title: '家用电器',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/家用电器.jpg',
        },
        {
          title: '3C数码',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/3C数码.jpg',
        },
        {
          title: '母婴用品',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/母婴用品.jpg',
        },
        {
          title: '鲜花绿植',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/鲜花绿植.jpg',
        },
        {
          title: '宠物用品',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/宠物用品.jpg',
        },
        {
          title: '手表配饰',
          imgURL:
            'https://duyi-bucket.oss-cn-beijing.aliyuncs.com/img/手表配饰.jpg',
        },
      ],
    };
  },
  directives: {
    scroll,
  },
  mounted() {
    this.scrollTo(this.index);
  },
  methods: {
    scrollTo(i) {
      if (this.move) return;
      this.index = i;
      this.$store.dispatch('fetchSideBar', this.menuList[i].title);
    },
  },
};
</script>

<style lang="less" scoped>
.one-tab-container {
  min-width: 375px;
  height: 94px;
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  .tab-item {
    width: 50px;
    height: 70px;
    margin: 10px 6px;
    .img-container {
      width: 50px;
      height: 50px;
      padding: 2.5px;
      border-radius: 50%;
      img {
        width: 45px;
        height: 45px;
        object-fit: cover;
        border-radius: 50%;
      }
    }
    .tab-title {
      text-align: center;
      font-size: 12px;
      height: 20px;
      line-height: 20px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      transform: translateY(5px);
    }
    &.active {
      .img-container {
        outline: 1px solid #d13193;
      }
      .tab-title {
        color: #fff;
        background: #d13193;
        border-radius: 30px;
        font-weight: 700;
      }
    }
  }
}
.one-tab-container::-webkit-scrollbar {
  width: 0;
  background: none;
}
</style>
